$(function() {
	initDP();
	initTitles();
	//initChkbox();
});
function initDP(){
	//$(".dpicker").datepicker({dateFormat: 'yy/mm/dd', showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.png'});
        $(".dpicker").datepicker({dateFormat: 'yy/mm/dd',showButtonPanel: true,changeMonth: true, changeYear: true, yearRange: '-10:+0', showOn: 'both', buttonImageOnly: true, buttonImage: '../../images/calendar.png'});
}
function initTitles(){
	postHandler('dailySales_search', {fun:'titles'}, function(val){
		initPopup(val);
		initPopupWindow();
		setTimeout('initChkbox()',500);
	});
}
function initPopup(val){
	var oWin = $('.checkbox_box_w');
	var html = '<label><input name="checkbox" type="checkbox" id="" checked="checked" value=""><strong style="color:#00aacc;">全部</strong></label><div class="checkbox_box"></div>'+
		'<input class="cancel button_" onclick="chkboxOk()" type="button" value="确定" />';
	oWin.html(html);
	var div = $('.checkbox_box');
	var tok = val.split('_####_');
	var idx = 0;
	for(var i=0;i<tok.length;i++){
		var tok2 = tok[i].split('_#_#_');
		var t1 = tok2[0];
		var tok3 = tok2[tok2.length-1].split('_###_');
		var tok4 = tok3[0].split('_##_');
		var _div = $('<div/>');
		var _html = '<label><input type="checkbox" name="checkbox" id="t_'+i+'" checked="checked" value="'+(tok4.length==1?tok4[0]:'')+'"><strong>'+t1+'</strong></label>';
		if(tok2.length>1 && tok4.length>1){
			_html += '<div class="checkbox_box" style="border-left-style: dotted;">';
			for(var j=0;j<tok3.length;j++){
				var tok5 = tok3[j].split('_##_');
				_html += '<div><label><input type="checkbox" name="checkbox" id="t_'+i+'_'+j+'_'+idx+'" checked="checked" value="'+tok5[1]+'">'+tok5[0]+'</label></div>';
				idx++;
			}
			_html += '</div>';
		}
		_div.html(_html).appendTo(div);
	}
}
function chkboxOk(){
	var oTar = $('.checkbox_box_w');
	var clone = oTar.clone();
	var oChks = clone.find('input:checkbox');
	var str='';
	oTar.find('input:checkbox').each(function(idx){
		var chk = $(this)[0].checked;
		var oDiv = oChks.eq(idx).closest('div');
		//if(oDiv.parent().hasClass('checkbox_box_w')) oDiv = oDiv.parent();
                //console.log(idx+", chk:"+chk);
		if(chk==false){
                    //if(oDiv.parent().hasClass('checkbox_box_w')){
                        oChks.eq(idx).parent().remove();
                    //}
		}else{
			if($(this).val().length>0){
			  str += ((str.length>0?',':'')+$(this).val());
                        }
                        oChks.eq(idx).remove();
                       // console.log(idx+", chk:"+chk+", val:"+$(this).val()+", str:"+str);
		}
                //console.log(idx+":clone:"+clone.html());
		if(idx==(oChks.length-1)){
			str = str.replace(/,/g,'_###_');
                        clone.find(".checkbox_box").each(function(){
                             var labels=$(this).find('label');
                             //console.log("labels:"+labels.length);
                             if (labels.length==0){
                                 $(this).remove();
                             }
                        });

			$('.show_select').html('<div class="checkbox_box_w">'+clone.find('.checkbox_box:eq(0)').html()+'<input type="hidden" id="shop_name" value="'+str+'"/></div>');
			clone.remove();
		}
	});

}
function query(){
	if($.trim($('#begin_time').val()).length>0 && !isDateStr($('#begin_time').val())){
		alert('开始日期有误');$('#begin_time').focus();return;
	}else if($.trim($('#end_time').val()).length>0 && !isDateStr($('#end_time').val())){
		alert('结束日期有误');$('#end_time').focus();return;
	}else if(compareDateStr($('#begin_time').val(), $('#end_time').val())<0){
		alert('结束日期应大于开始日期');$('#end_time').focus();return;
	}
	var clone = $('.show_select').clone();
	var oLab = clone.find('label');
	for(var i=0;i<oLab.length;i++){
		if(oLab.eq(i).attr('hasChildren')) oLab.eq(i).html('');
	}
	str = $.trim(clone.html().replace(/<.*?>/g,' ').replace(/[\r\n]/g,' ')).replace(/\s+/g,'_###_');
	clone.remove();
	var datas = {fun:'query'};
	var oIpts = $('input[type="text"]');
	for(var i=0;i<oIpts.length;i++){
		if($.trim(oIpts.eq(i).val()).length>0)
			if(oIpts.eq(i)[0] && $.trim(oIpts.eq(i).val()).length>0)
				datas[oIpts.eq(i)[0].id] = oIpts.eq(i).val();
	}
	if(str.length>0) datas['shopid'] = $('#shop_name').val();
	else{
		//alert('店铺未选取'); return false;
	}
	datas['begin_time'] = $('#begin_time').val();
	datas['end_time'] = $('#end_time').val();
	// postHandler('dailySales_search',datas, function(val){
	// 	window.location='dailySales.html';
	// 	return false;
	// });
	var url = "dailySales?";
	for (var i in datas) {
		url += i + '=' + datas[i] + '&';
	}
	$.fileDownload(url);
}
